package com.nineclock.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.common.utils.DateTimeUtil;
import com.nineclock.sign.dto.SignDTO;
import com.nineclock.sign.mapper.SignInfoMapper;
import com.nineclock.sign.mapper.SignPictureMapper;
import com.nineclock.sign.pojo.SignInfo;
import com.nineclock.sign.pojo.SignPicture;
import com.nineclock.sign.service.SignUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SignUserServiceImpl implements SignUserService {
    @Autowired
    SignInfoMapper signInfoMapper;
    @Autowired
    SignPictureMapper signPictureMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<SignDTO> CurrentUserByMonth(String dateStr) {

        LambdaQueryWrapper<SignInfo> signDTOQW = new LambdaQueryWrapper<>();
        signDTOQW.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
        signDTOQW.eq(SignInfo::getCompanyUserId, CurrentUserHolder.get().getCompanyUserId());
        signDTOQW.eq(SignInfo::getCreateMonth, dateStr);
        List<SignInfo> signInfoList = signInfoMapper.selectList(signDTOQW);

        if (signInfoList != null) {
            List<SignDTO> signDTOS = BeanHelper.copyWithCollection(signInfoList, SignDTO.class);

            for (SignDTO signDTO : signDTOS) {
                LambdaQueryWrapper<SignPicture> signPictureQW = new LambdaQueryWrapper<>();
                signPictureQW.eq(SignPicture::getSignId, signDTO.getId());
                List<SignPicture> signPictureList = signPictureMapper.selectList(signPictureQW);

                signDTO.setUserName(CurrentUserHolder.get().getUsername());
                if (signPictureList != null) {
                    List<String> signUrlList = signPictureList.stream().map(signPicture -> {
                        String signUrl = signPicture.getUrl();
                        return signUrl;
                    }).collect(Collectors.toList());
                    String urlStr = signUrlList.toString();

                    String substring = urlStr.substring(1, urlStr.length() - 1);
                    signDTO.setImages(substring);
                    signDTO.setSignPictureUrlList(signUrlList);

                }
                String mobile = CurrentUserHolder.get().getMobile();
                Date date = new Date();
                String createDate = DateTimeUtil.dateToStr(date,DateTimeUtil.TIME_FORMAT_2);
                String dayKey = createDate + mobile;
                String reidsDayCount = (String)redisTemplate.boundValueOps(dayKey).get();
                if (reidsDayCount == null){
                    LambdaQueryWrapper<SignInfo> signInfoDayCount = new LambdaQueryWrapper<>();
                    signInfoDayCount.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
                    signInfoDayCount.eq(SignInfo::getCompanyUserId, CurrentUserHolder.get().getCompanyUserId());
                    signInfoDayCount.eq(SignInfo::getCreateDate,createDate);
                    Integer intDayCount = signInfoMapper.selectCount(signInfoDayCount);
                    String dayCount = String.valueOf(intDayCount);
                    signDTO.setDayCount(dayCount);
                    String createDayCount = signDTO.getCreateDate()+mobile;
                    redisTemplate.boundValueOps(createDayCount).set(dayCount);
                }else {
                    signDTO.setDayCount(reidsDayCount);
                }

                String createMonth = signDTO.getCreateMonth();
                String monthKey = createMonth + mobile;
                String redisMonthCount = (String)redisTemplate.boundValueOps(monthKey).get();
                if (redisMonthCount == null){
                    LambdaQueryWrapper<SignInfo> signInfoMonthCount = new LambdaQueryWrapper<>();
                    signInfoMonthCount.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
                    signInfoMonthCount.eq(SignInfo::getCompanyUserId, CurrentUserHolder.get().getCompanyUserId());
                    signInfoMonthCount.eq(SignInfo::getCreateMonth,signDTO.getCreateMonth());
                    Integer intMonthCount = signInfoMapper.selectCount(signInfoMonthCount);
                    String monthCount = String.valueOf(intMonthCount);
                    signDTO.setMonthCount(monthCount);
                    String createMonthCount = signDTO.getCreateMonth()+mobile;
                    redisTemplate.boundValueOps(createMonthCount).set(monthCount);
                }else {
                    signDTO.setMonthCount(redisMonthCount);

                }
            }
            return signDTOS;
        }


        return new ArrayList<>();
    }
}
